package com.it.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.it.UserService;
import com.it.bean.ConstEnum;
import com.it.dao.ArticleMapper;
import com.it.dao.FollowingMapper;
import com.it.dao.ProductMapper;
import com.it.dao.UserMapper;
import com.it.pojo.Following;
import com.it.pojo.Product;
import com.it.pojo.User;
import com.it.pojo.params.user.UserCenterInfoQuery;
import com.it.pojo.vo.article.ArticleByUserIdVO;
import com.it.pojo.vo.product.ProductSpaceVO;
import com.it.pojo.vo.user.UserCenterVO;
import com.it.pojo.vo.user.UserFollowVO;
import com.it.pojo.vo.user.UserSpaceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;


    @Resource
    private FollowingMapper followingMapper;

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private ProductMapper productMapper;

    @Override
    public Map<String, Object> getUserVO(Integer userId) {

        UserSpaceVO userSpaceVO = new UserSpaceVO();
        Map<String, Object> map = new HashMap<>();
        BeanUtils.copyProperties(userMapper.selectById(userId), userSpaceVO);
        Integer followingTotal = followingMapper.getFollowingTotal(userId); // 关注的数量
        Integer fanTotal = followingMapper.getFanTotal(userId); // 粉丝的数量
        Integer articleTotal = articleMapper.getArticleTotalByUserId(userId);// 文章的数量
        List<ArticleByUserIdVO> articleByUserIdVOList = articleMapper.getArticleVOByUserId(userId);// 喵街的信息
        List<UserFollowVO> fansUserInfoList = getFansUserInfo(userId); // 粉丝的用户信息
        List<UserFollowVO> followUserInfo = getGuanZhuUserInfo(userId); // 关注的用户信息
//                                                         获取漫展的信息
        List<ProductSpaceVO> productSpaceVOList = productMapper.getProductInfoByUserId(userId);
        map.put("followingTotal", followingTotal == null ? 0 : followingTotal);
        map.put("fanTotal", fanTotal == null ? 0 : fanTotal);
        map.put("userSpaceVO", userSpaceVO);
        map.put("articleTotal", articleTotal == null ? 0 : articleTotal);
        map.put("articleByUserIdVOList", articleByUserIdVOList);
        map.put("fansUserInfoList", fansUserInfoList);
        map.put("followUserInfo", followUserInfo);
        map.put("productSpaceVOList", productSpaceVOList);
        return map;
    }

    @Override
    public UserCenterVO getUserCenterVO(Integer userId) {
        UserCenterVO userCenterVO = userMapper.getUserCenterVO(userId);

        return userCenterVO;
    }

    @Override
    public int updateUserCenterInfo(UserCenterInfoQuery userCenterInfoQuery) {
        User user = new User();
        BeanUtils.copyProperties(userCenterInfoQuery, user);
        user.setProvinceId(Integer.parseInt(userCenterInfoQuery.getProvinceId()));
        user.setCityId(Integer.parseInt(userCenterInfoQuery.getCityId()));
        user.setGmtModified(new Date());
        return userMapper.updateById(user);

    }

    @Override
    public int updatePwd(Integer userId, String password, String rePwd) {
        User user = userMapper.selectById(userId);
        if (user != null) {
            String md5Pwd = SecureUtil.md5(password + ConstEnum.PWD_SALT.getResult());
            if (md5Pwd.equals(user.getPassword())) {
                String reMd5 = SecureUtil.md5(rePwd + ConstEnum.PWD_SALT.getResult());
                user.setPassword(reMd5);
                user.setGmtModified(new Date());
                return userMapper.updateById(user);

            }
        }
        return 0;

    }

    private List<UserFollowVO> getGuanZhuUserInfo(Integer userId) {
        LambdaQueryWrapper<Following> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Following::getUserFollowingId, userId);// 查询我关注的用户id
        List<Following> followingList = followingMapper.selectList(wrapper); // 查询我关注的用户id
        List<Integer> listFansInteger = new ArrayList<>();
        followingList.forEach(e -> {
            System.out.println("e = " + e);

            listFansInteger.add(e.getUserId());
        });
        if (listFansInteger.size() == 0) {
            listFansInteger.add(0);
        }
        List<UserFollowVO> followVOList = userMapper.listUserFansByInUserId(listFansInteger);
//        List<UserFollowVO> followVOList = new ArrayList<>();

        return followVOList;
    }


    private List<UserFollowVO> getFansUserInfo(Integer userId) {
        LambdaQueryWrapper<Following> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Following::getUserId, userId);
        List<Following> followingList = followingMapper.selectList(wrapper); // 查询粉丝的用户id

        List<Integer> listFansInteger = new ArrayList<>();
        followingList.forEach(e -> {
            System.out.println("e 粉丝 = " + e);
            listFansInteger.add(e.getUserFollowingId()); // 得到粉丝的id
        });
        if (listFansInteger.size() == 0) {
            listFansInteger.add(0);
        }
        List<UserFollowVO> followVOList = userMapper.listUserFansByInUserId(listFansInteger);
//        List<UserFollowVO> followVOList = new ArrayList<>();
        return followVOList;

    }
}
